package org.dromara.business.controller;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.UserStakingVo;
import org.dromara.business.domain.bo.UserStakingBo;
import org.dromara.business.service.IUserStakingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户质押记录
 *
 * @author Eric
 * @date 2025-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/userStakings")
public class UserStakingsController extends BaseController {

    private final IUserStakingService userStakingsService;

    /**
     * 查询用户质押记录列表
     */
    @SaCheckPermission("business:userStakings:list")
    @GetMapping("/list")
    public TableDataInfo<UserStakingVo> list(UserStakingBo bo, PageQuery pageQuery) {
        return userStakingsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户质押记录列表
     */
    @SaCheckPermission("business:userStakings:export")
    @Log(title = "用户质押记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserStakingBo bo, HttpServletResponse response) {
        List<UserStakingVo> list = userStakingsService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户质押记录", UserStakingVo.class, response);
    }

    /**
     * 获取用户质押记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:userStakings:query")
    @GetMapping("/{id}")
    public R<UserStakingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userStakingsService.queryById(id));
    }

    /**
     * 新增用户质押记录
     */
    @SaCheckPermission("business:userStakings:add")
    @Log(title = "用户质押记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserStakingBo bo) {
        return toAjax(userStakingsService.insertByBo(bo));
    }

    /**
     * 修改用户质押记录
     */
    @SaCheckPermission("business:userStakings:edit")
    @Log(title = "用户质押记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserStakingBo bo) {
        return toAjax(userStakingsService.updateByBo(bo));
    }

    /**
     * 删除用户质押记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:userStakings:remove")
    @Log(title = "用户质押记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userStakingsService.deleteWithValidByIds(List.of(ids), true));
    }

    @GetMapping("processRebates")
    public R<Void> processRebates(Date dateTime) {
        LocalDateTime ldt = dateTime.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
        userStakingsService.processRebates(ldt);
        return toAjax(true);
    }
}
